const express = require('express')
const router = express.Router()
const pool = require('../pool')
const { host } = require('../router/host')
const query = require('../router/utils')
const multer = require('multer')
const {
    resSuccessData,
    resSuccess,
    resError } = require('../utils/index')

    // 记谱列表
router.post('/list', (req, res) => {
    let page = req.body.page
    let keywords = req.body.keywords
    let condition = ''
    let params = []
    if (keywords) {
        condition = `where j.comment like '%${keywords}%'`
    }
    let count = 6
    let start = (parseInt(page) - 1) * count
    let sql = `select j.comment,j.photo,j.open_id,j.id,u.nickName from jipu as j inner join userinfo as u on j.open_id=u.open_id ${condition} limit ${start},${count}`
    pool.query(sql, params, (err, result) => {
        if (err) throw err
        result.forEach(el => {
            el.photo = host + "/" + el.photo
        });
        if (keywords) {
            condition = `where comment like '%${keywords}%'`
        }
        sql = `select count(id) as count from jipu ${condition}`
        pool.query(sql, [], (err, result1) => {
            if (err) throw err
            res.send({ code: 1, data: result, total: result1[0].count })
        })
    })
})
// 新增记谱上传图片路径
// 上传分类下的乐谱图片
var storage2 = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, 'image/comment/')
    },
    filename: function (req, file, cb) {
        cb(null, file.originalname)
    }
})
var upload3 = multer({ storage: storage2 })
router.post('/addDetailFile', upload3.single('file'), async (req, res) => {
    // body 包含表单信息
    const photo = req.file.path.replace(/\\/g, '/').slice(6);
    // 将这个数据返回给前端,前端上传查到数据
    res.send(photo)

})
// 新增记谱
router.post('/add', async (req, res) => {
    let { comment, photo ,open_id} = req.body
    let sql = 'insert into jipu (comment,photo,open_id) values (?,?,?)';
    let ret = await query(sql, [comment, photo,open_id])
    if (ret.affectedRows > 0) {
        res.send({ code: 1, msg: '增加成功' })
    }
})
//删除接口
router.post('/del', async (req, res) => {
    // 传入数组
    let dels = req.body;
    // console.log(dels)
    if (dels.length) {
        dels = dels.join(',');
        // 删除
        let sql = `delete from jipu where id in(${dels})`;
        const ret = await query(sql, []);
        if (ret.affectedRows > 0) {
            resSuccess(res)
        } else {
            resError(res, '删除失败')
        }
    } else {
        resError(res, '删除参数不能为空')
    }
})
// 获取特定曲谱的详情
router.post('/detail', async (req, res) => {
    let { id } = req.body
    let sql = 'select * from jipu where id=?'
    let ret = await query(sql, [id])
    if (ret.length) {
        res.send({ code: 1, data: ret[0] })
    } else {
        res.send({ code: 0, msg: '查找失败' })
    }
})

// 编辑后的分类上传
router.post('/update', async (req, res) => {
    let { comment, photo, id } = req.body
    let sql = 'update jipu set comment=?,photo=? where id=?'
    let ret = await query(sql, [comment, photo, id])
    if (ret.affectedRows > 0) {
        resSuccess(res)
    } else {
        resError(res, '修改失败')
    }

})



module.exports = router